package lvbu.wang.lvbuoperator.manager;

import android.content.Context;

import com.qing.library.utils.NetWorkUtil;
import com.qing.library.utils.ToastUtil;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import lvbu.wang.lvbuoperator.R;
import lvbu.wang.lvbuoperator.core.BaseActivity;
import lvbu.wang.lvbuoperator.services.BluetoothLEService;


/**
 * Created by DaiQing.Zha on 2016/6/12 0012.
 * Email:1642674371@qq.com
 * Description:
 */
public class ThreadPool {
    private ExecutorService service;
    public static final int HTTP = 0;   //http请求
    public static final int BLE = 1;    //蓝牙请求
    public static final int HTTPANDBLE = 2; //http和蓝牙请求

    private ThreadPool() {
        int num = Runtime.getRuntime().availableProcessors();
        service = Executors.newFixedThreadPool(num * 2);
    }

    private static final ThreadPool manager = new ThreadPool();

    public static ThreadPool getInstance() {
        return manager;
    }

    /**
     * 向西安城池中添加任务---添加之前不检查网络
     * @param runnable
     */
    public void addTask(Runnable runnable) {

        service.execute(runnable);
    }

    /**
     * 向线程池中添加任务---添加之前检查网络
     * @param runnable
     * @param context
     */
    public void addTask(Runnable runnable,Context context) {
        if (!NetWorkUtil.NETWORK){
            ToastUtil.showShortToast(context,context.getString(R.string.toast_comm_ioError));
            return;
        }
        service.execute(runnable);
    }

    /**
     * 向线程池中添加任务---添加之前检查网络跟蓝牙连接
     * @param runnable
     * @param context
     * @param flag
     */
    public void addTask(Runnable runnable,Context context,int flag) {

        if (flag == HTTPANDBLE){

            if (!NetWorkUtil.NETWORK && BaseActivity.getMotorDefault_bluetooth_connect_state(context) != BluetoothLEService.Bluetooth_CONNECTED){
                ToastUtil.showShortToast(context,context.getString(R.string.toast_comm_plOpenNetworkOrBle));
                return;
            }
        }
        service.execute(runnable);
    }
}
